﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HomeStayWebCP.Models;
using DAL;
using HomeStayWebCP.Models.BusinessObject;
using HomeStayWebCP.Utility;

namespace HomeStayWebCP.Controllers
{
    public class HousesController : Controller
    {
        //
        // GET: /Houses/

        public ActionResult ViewMap(int id)
        {
            HouseModel model = new HouseModel();
            //get from database
            List<Store_House_GetHouseOrdinateAndPostTitle_Result> kk = model.getIMapInforOnProvince(id);
            //removed duplicate ordinate list
            List<Store_House_GetHouseOrdinateAndPostTitle_Result> l = new List<Store_House_GetHouseOrdinateAndPostTitle_Result>();

            List<string> ordinate = new List<string>();
            foreach (var h in kk)
            {
                if (!ordinate.Contains(h.Ordinate))
                {
                    ordinate.Add(h.Ordinate);
                    l.Add(h);
                }
            }
            List<MarkerInformation> list = new List<MarkerInformation>();
            string posTitles = "";
            string postImages = "";
            string postTypes = "";
            string pricesperday = "";
            string pricesperweek = "";
            string pricespermonth = "";
            string houseAvailables = "";
            foreach (var i in l)
            {
                MarkerInformation m = new MarkerInformation();
                m.houseID = i.HouseID;
                String td = i.Ordinate;
                String[] latlong = td.Split(',');
                m.lat = latlong[0];
                m.lon = latlong[1];
                m.posID = i.PostID;
                m.posTitle = i.Title;
                m.PostAvatar = Util.imagePath + i.PostAvatar;
                m.TypeOfHouse = i.TypeOfHouse;

                m.houseStatus = i.IsAvailable == true ? "Available" : "Unavailable";
                houseAvailables += m.houseStatus+ "%%";

                m.priceperday = i.PricePerDay > 0 ? i.PricePerDay.ToString() +" USD" : "N/A";
                pricesperday += m.priceperday + "%%";

                m.priceperweek = i.PricePerWeek > 0 ? i.PricePerWeek.ToString() + " USD" : "N/A";
                pricesperweek += m.priceperweek + "%%";

                m.pricepermonth = i.PricePerMonth > 0 ? i.PricePerMonth.ToString() + " USD" : "N/A";
                pricespermonth += m.pricepermonth + "%%";

                posTitles += i.Title + "%%";

                postImages += Util.imagePath + i.PostAvatar + "%%";
                postTypes += i.TypeOfHouse + "%%";

                //add a maker infor to list result
                list.Add(m);
            }
            if (posTitles.Length > 2)
            {
                posTitles = posTitles.Remove(posTitles.Length - 2);
            }
            if (postImages.Length > 2)
            {
                postImages = postImages.Remove(postImages.Length - 2);
            }
            if (postTypes.Length > 2)
            {
                postTypes = postTypes.Remove(postTypes.Length - 2);
            }
            if (houseAvailables.Length > 2)
            {
                houseAvailables = houseAvailables.Remove(houseAvailables.Length - 2);
            }
            if (pricesperday.Length > 2)
            {
                pricesperday = pricesperday.Remove(pricesperday.Length - 2);
            }
            if (pricesperweek.Length > 2)
            {
                pricesperweek = pricesperweek.Remove(pricesperweek.Length - 2);
            }
            if (pricespermonth.Length > 2)
            {
                pricespermonth = pricespermonth.Remove(pricespermonth.Length - 2);
            }
            ViewBag.listMarkerInfomation = list;
            ViewBag.posTitles = posTitles;
            ViewBag.postImages = postImages;
            ViewBag.postTypes = postTypes;

            ViewBag.houseAvailables = houseAvailables;
            ViewBag.pricesperday = pricesperday;
            ViewBag.pricesperweek = pricesperweek;
            ViewBag.pricespermonth = pricespermonth;

            ViewBag.numberHouse = list.Count;

            return PartialView("ViewOnMap");
        }
        public ActionResult Search(HouseModel house)
        {
            return null;
        }
       
    }
}
